package com.minstone.common.util;

import java.util.Map;

import org.apache.log4j.Logger;

/**
* <pre>
* Map工具类。
* </pre>
* @author HuangBaixian
* @version 1.0
* @since 2016年3月10日下午5:37:10
*
*/
public class MapUtils {
	private static final Logger LOGGER = Logger.getLogger(MapUtils.class);

	/**
	 * @param param
	 * @param paramName
	 * @param defaultValue
	 * @return
	 */
	public static String getString(Map param, String paramName, String defaultValue){
		Object obj = param.get(paramName);
		return StringUtil.isBlank(obj)? defaultValue : obj.toString();
	}
	
	/**
	 * @param param
	 * @param paramName
	 * @return
	 */
	public static String getString(Map param, String paramName){
		return getString(param, paramName, "");
	}
	
	/**
	 * @param param
	 * @param paramName
	 * @param defaultValue
	 * @return
	 */
	public static int getInt(Map param, String paramName, int defaultValue){
		Object obj = param.get(paramName);
		if(obj == null){
			return defaultValue;
		}else{
			try{
				return Integer.parseInt(obj.toString());
			}catch(Exception e){
				LOGGER.error(e.getMessage(), e);
				return defaultValue;
			}
		}
	}
	
	/**
	 * @param param
	 * @param paramName
	 * @return
	 */
	public static int getInt(Map param, String paramName){
		return getInt(param, paramName, 0);
	}
	
	/**
	 * @param param
	 * @param paramName
	 * @param defaultValue
	 * 空字符串时，默认为指定数据
	 * @return
	 */
	public static int getInteger(Map param, String paramName, int defaultValue){
		Object obj = param.get(paramName);
		if(obj == null){
			return defaultValue;
		}else{
			try{
				if("".equals(obj.toString())){
					return defaultValue;
				}
				return Integer.parseInt(obj.toString());
			}catch(Exception e){
				LOGGER.error(e.getMessage(), e);
				return defaultValue;
			}
		}
	}
	/**
	 * @param param
	 * @param paramName
	 * @return
	 */
	public static Integer getInteger(Map param, String paramName){
		Object obj = param.get(paramName);
		if(obj == null){
			return null;
		}else{
			try{
				if("".equals(obj.toString())){
					return null;
				}
				return Integer.parseInt(obj.toString());
			}catch(Exception e){
				LOGGER.error(e.getMessage(), e);
				return null;
			}
		}
	}
	
	/**
	 * @param param
	 * @param paramName
	 * @param defaultValue
	 * @return
	 */
	public static long getLong(Map param, String paramName, long defaultValue){
		Object obj = param.get(paramName);
		if(obj == null){
			return defaultValue;
		}else{
			try{
				return Long.parseLong(obj.toString());
			}catch(Exception e){
				LOGGER.error(e.getMessage(), e);
				return defaultValue;
			}
		}
	}
	
	
	
	/**
	 * @param param
	 * @param paramName
	 * @return
	 */
	public static long getLong(Map param, String paramName){
		return getLong(param, paramName, 0L);
	}
}
